package com.linghang.association.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.MessageDigest;
/**
 * @author LinyaT052
 * @date 2022年07月24日 16:35
 * MD5加密工具类
 */
public class MD5Util {
    private static Logger LOG = LoggerFactory.getLogger(MD5Util.class);
    /**
     * Md5加密 返回byte[]
     *
     * @param s
     * @return
     */
    public static byte[] getMD5(String s) {
        MessageDigest algorithm;
        try {
            algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(s.getBytes("UTF-8"));
            byte[] messageDigest = algorithm.digest();
            return messageDigest;
        } catch (Exception e) {
            LOG.info("MD5 Error...", e);
        }
        return null;
    }

    /**
     * byte[]解密 返回string
     *
     * @param hash
     * @return
     */
    private static final String toHex(byte hash[]) {
        if (hash == null) {
            return null;
        }
        StringBuffer buf = new StringBuffer(hash.length * 2);
        int i;

        for (i = 0; i < hash.length; i++) {
            if ((hash[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString(hash[i] & 0xff, 16));
        }
        return buf.toString();
    }
}
